<?php

namespace App\Console\Commands;

use App\Episode;
use App\Show;
use GuzzleHttp;
use Illuminate\Console\Command;
use Symfony\Component\DomCrawler\Crawler;


class SpiderEztv extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'spider:eztvep';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '爬取下载资源eztv美剧网站';


    protected $client;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->client = new GuzzleHttp\Client();

        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $xmlURL = 'https://eztv.ag/ezrss.xml';
//        $content = file_get_contents($xmlURL);
//
//        $xmlObj = simplexml_load_string($content);
//        dd($xmlObj);

        $webURl = 'https://eztv.ag/';

        $res = $this->client->request('GET', $webURl, ['headers' => ['user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36', 'referer' => 'https://www.google.co.kr/',]]);
        $html = $res->getBody()->getContents();
        $crawler = new Crawler($html);
        $crawler->filter('[name="hover"].forum_header_border')->each(function(Crawler $row,$i){
            $show_name = $row->filterXPath('//td[1]/a[1]/img[1]')->attr('title');
            $show_name = str_ireplace('Show Description about',' ',$show_name);
            $show_name = str_ireplace('Torrents',' ',$show_name);
            $show_name = trim($show_name);
            echo($show_name . '\n');

            $eztv_name = $row->filterXPath('//td[2]')->text();
            $eztv_name = trim($eztv_name);
            echo($eztv_name . '|||||');

            $episode = Episode::firstOrNew(['raw_name'=>$eztv_name]);
            $episode->provider = 'eztv';
            $node = $row->filterXPath('//td[1]/a');
            if ($node->count()>1) {
                $episode->url_tvmaze = $row->filterXPath('//td[1]/a[2]')->attr('href');
//                dd($episode->url_tvmaze);
            }
            $providerURL = $row->filterXPath('//td[1]/a[1]')->attr('href');
            $episode->url_provider = $providerURL;

            $row->filterXPath('//td[3]/a')->each(function(Crawler $aLink,$i) use ($episode) {
                $value = $aLink->attr('href');
                if(str_contains($value,'torrent')) {
                    $episode->url_torrent = $value;
                }
                if(str_contains($value,'magnet')) {
                    $episode->url_magnet = $value;
                }
            });

            $size = $row->filterXPath('//td[4]')->text();
            $episode->size = $size;
            $show_name = str_ireplace('(','',$show_name);
            $show_name = str_ireplace(')','',$show_name);

            $show = Show::firstOrCreate(['name_en'=>$show_name]);

            if($episode->show_id<1) {
                $episode->show_id = $show->id;
            }
            $episode->name = filterEpisodeRawName($eztv_name);
            $episode->save();

            $show->updated_at = $episode->created_at;

            $show->save();
        });
    }
}
